package com.xjnt.mnui.controller;

import java.util.Date;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.CRUDController;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.message.RetMsg;
import com.xjnt.mnui.entity.SubjectTemplate;
import com.xjnt.mnui.service.SubjectTemplateService;

@Router(name="/mnui/sub/template", view="/mnui/subjectTemplate")
public class SubjectTemplateController extends CRUDController<SubjectTemplate, SubjectTemplateService> {

	@AutoInject
	private SubjectTemplateService subjectTemplateService;
	
	@Override
	protected String getBizName() {
		return "模板";
	}

	@Override
	protected Class<SubjectTemplate> getClazz() {
		return SubjectTemplate.class;
	}

	@Override
	protected SubjectTemplateService getService() {
		return subjectTemplateService;
	}	
	@Override
	public void doSave(){
		RetMsg retMsg = new RetMsg();
		try {
			SubjectTemplate model = getModel(getClazz(),"subjectTemplate");
			//对实体进行操作
			model.set("create_time", new Date());
			model.set("type", 0);//默认状态：启用
			boolean flag = getService().save(model);
			if (flag) {
				retMsg.pushOk("保存成功");
			} else {
				retMsg.pushError("保存失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("保存" + getBizName(), "发生异常：", e);
		}
	}
	@Override
	public void doUpdate(){
		RetMsg retMsg = new RetMsg();
		try {
			SubjectTemplate model = getModel(getClazz(),"subjectTemplate");
			model.set("update_time", new Date());
			boolean flag = getService().update(model);
			//对实体进行操作
			if (flag) {
				retMsg.pushOk("更新成功");
			} else {
				retMsg.pushError("更新失败");
			}
			renderJson(retMsg);
		} catch (Exception e) {
			throw new BusinessException("更新" + getBizName(), "发生异常：", e);
		}
	}
	/**
	 * 数据删除
	 */
	@Before(Tx.class)
	@Override
	public void deletes() {
		RetMsg retMsg = new RetMsg();
		try {
			String idStr = getPara("ids");
			Object[] ids = idStr.split(",");
			boolean type = false;//删除的记录中是否存在已被专题使用的模板。
			for (Object id : ids) {
				Record t = getService().findTemplateSumById(id);
				if(!"0".equals(t.get("sum").toString())){
					type = true;
					break;
				}
			}
			if(type){
				retMsg.pushError("删除的数据中存在被专题使用的模板，请确认后操作。");
				renderJson(retMsg);
			}else{
				boolean flag = getService().deleteByIds(ids);
				if (flag) {
					retMsg.pushOk("批量删除成功");
				} else {
					retMsg.pushError("批量删除失败");
				}
				renderJson(retMsg);
			}
		} catch (Exception e) {
			throw new BusinessException("批量删除" + getBizName(), "发生异常：", e);
		}
	}
	/**
	 * 启用/停用 ajax方法
	 */
	public void enableAJax(){
		RetMsg retMsg = new RetMsg();
		String able = getPara("type");//0:启用,1:停用
		String idStr = getPara("ids");
		Object[] ids = idStr.split(",");
		if("0".equals(able)){
			//启用
			boolean flag = getService().enables(ids);//批量启用
			if (flag) {
				retMsg.pushOk("批量启用成功");
			} else {
				retMsg.pushError("批量启用失败");
			}
			renderJson(retMsg);
		}else{
			//停用
			boolean type = false;//操作的记录中是否存在已被专题使用的模板。
			for (Object id : ids) {
				Record t = getService().findTemplateSumById(id);
				if(!"0".equals(t.get("sum").toString())){
					type = true;
					break;
				}
			}
			if(type){
				retMsg.pushError("停用的数据中存在被专题使用的模板，请确认后操作。");
				renderJson(retMsg);
			}else{
				boolean flag = getService().disables(ids);//批量停用
				if (flag) {
					retMsg.pushOk("批量停用成功");
				} else {
					retMsg.pushError("批量停用失败");
				}
				renderJson(retMsg);
			}
		}
	}
}
